<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head th:include="inc/home::header">
</head>

<link type="text/css" th:href="@{/css/content.css}" rel="stylesheet" />
	<body>
		<!--返回-->
		<div class="back"  onclick="loadPage('/Spare/spare')"><img src="/images/back.png" /></div>
		<div th:include="header :: header"></div>
		<div class="header-title">
			<span>添加备件</span>
		</div>
		<div class="spare-a">
			<div class="save-bg"><a href="#" class="spare-tj" onclick="Save()">保 存</a></div>
			<div class="spare-add">
			<form id="spareForm">
			<input type="text" readonly="readonly" style="display: none" name="id" />
			<input type="text" class="spare-add-input" style="display: none" id="spareQuantity"  name="spareQuantity" value="0"/>
				<ul>
					<li>
						<div class="spare-addl"><span class="star">*</span>编号/型号：</div>
						<div class="spare-addr"><input type="text" class="spare-add-input" id="spareCode"  name="spareCode"/></div>
					</li>
					<li>
						<div class="spare-addl"><span class="star">*</span>序列号：</div>
						<div class="spare-addr"><input type="text" class="spare-add-input"  id="spareNo"  name="spareNo"/></div>
					</li>
					<li>
						<div class="spare-addl"><span class="star">*</span>名称：</div>
						<div class="spare-addr"><input type="text" class="spare-add-input"  id="spareName"  name="spareName"/></div>
					</li>					
					<li>
						<div class="spare-addl">供应商：</div>
						<div class="spare-addr">
							<select class="spare-add-dbselect" id="spareSupplier"  name="spareSupplier">
								<option value=""></option>
								
							</select>
							<div class="select-arrow"></div>
						</div>
					</li>
					<li>
						<div class="spare-addl">类型：</div>
						<div class="spare-addr">
							<select class="spare-add-dbselect" id="spareType"  name="spareType">
								<option value=""></option>
								
							</select>
							<div class="select-arrow"></div>
						</div>
					</li>
				</ul>
				</form>
			</div>
		</div>
	</body>
	<script th:inline="javascript">  
/*<![CDATA[*/ 
  //判断序号的唯一性
  $(function(){
	  //加载备件信息
	  var spare = [[${spare}]];
      if(spare!=null){
          loadData(spare);
      }
      $('#spareNo').blur(function(){
           var spareNo1=$(this).val();
          $.ajax({
              type:"post",
              data:"t_spareNo="+spareNo1,
              url:"/Spare/spareNoCheck",
             success:function(data){
	if(data.length>0&&spare==null){
		$.Zebra_Dialog('<div class="prompt"><div class="error-icon"></div><div class="success-title">该序号已存在,请重新填写</div></div>', {
			   'title':'提示',
			   'buttons' : [ {
					caption : '确定',
					callback : function() {
						$('#spareNo').val("");
					}
				} ]
			});
	}else if(data.length>0&&spare!=null){
	                	 if(data[0].spareNo!=spare.spareNo){
	                		 $.Zebra_Dialog('<div class="prompt"><div class="error-icon"></div><div class="success-title">该序号已存在,请重新填写</div></div>', {
	      					   'title':'提示',
	      					   'buttons' : [ {
	      							caption : '确定',
	      							callback : function() {
	      								$('#spareNo').val("");
	      							}
	      						} ]
	      					});
		                    } 
                 }
              }
          })
      })
 })



 function Save(){
	   var data= getDataJson("spareForm");
	   var obj = JSON.stringify(data);
	   console.log(data);
	   if(data.spareCode!=""&&data.spareName!=""&&data.spareNo!=""){
		   AjaxWebTableInfo("/Spare/saveOrUpdate",data);
	   }else{
		   $.Zebra_Dialog('<div class="prompt"><div class="error-icon"></div><div class="success-title">必填项不能为空!</div></div>', {
	            'title':'提示',
	            'buttons':['确定']
	         });
	   }
    
	 }
	      
	function AjaxWebTableInfo(url , obj){
		 $.ajax({
		     type: "post",
		     dataType: "json",
		     url: url,
		     data:obj,
		     success: function (data, status) {
		    	 		$.Zebra_Dialog(
								'<div class="prompt"><div class="success-icon"></div><div class="success-title">保存成功</div></div>',
								{
									'title' : '提示',
									'show_close_button' : false,
									'buttons' : [ {
										caption : '确定',
										callback : function() {
											window.location.href="/Spare/spare"
											
										}
									} ]
								});
		     },
		     error: function (data) {
		    	 $.Zebra_Dialog('<div class="prompt"><div class="error-icon"></div><div class="success-title">保存失败!</div></div>', {
			            'title':'提示',
			            'buttons':['确定']
			         });
		     }
		 });	
	}
	//加载供应商和备件类型
	$(function() { 
	 $.ajax({
	    type: 'GET',
	    url: '/Spare/spareSelect?timestamp='+new Date().getTime(),
	    success: function (data) {
	        var data = eval(data);
	        var suppliers = [];
	        var suppliers1 = [];
	        var types = [];
	        var types1 = [];
	        var proOPts1 = "";
	        var proOPts2 = "";
	        for(var i = 0; i < data.length; i++){
	        	suppliers.push(data[i].spareSupplier);
				types.push(data[i].spareType);
				if(suppliers1.indexOf(suppliers[i])<0){
				 suppliers1.push(suppliers[i]);
				 }	    
				if(types1.indexOf(types[i])<0){
				  types1.push(types[i]);
            }
	        }	         
	        for(var i = 0; i < suppliers1.length; i++){
	            proOPts1 += "<option value='" + suppliers1[i]+ "'>" + suppliers1[i] + "</option>";
	        }
	        for(var i = 0; i < types1.length; i++){
	            proOPts2 += "<option value='" + types1[i]+ "'>" + types1[i] + "</option>";
	        }
	        $("#spareSupplier").append(proOPts1);	
	        $("#spareType").append(proOPts2);	
	        $('#spareSupplier').editableSelect({
	    		effects: 'fade',
	    		duration: 200	    		
	    	});
	    	$('#spareType').editableSelect({
	    		effects: 'fade',
	    		duration: 200	
	    	});
	    }
	});  
	
	});
	

/*]]>*/  
</script>	
</html>
